package com.achilles.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

import com.achilles.playtask.R;

/**
 * Created by AchillesL on 2016-9-24.
 */
public class ScoreView extends TextView {

    private int mScore;
    public ScoreView(Context context) {
        this(context,null);
    }

    public ScoreView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public ScoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        this.setTextColor(ContextCompat.getColor(getContext(),R.color.colorPrimaryDark));
        this.setTextSize(TypedValue.COMPLEX_UNIT_SP,70);
    }

    public int getScore() {
        return mScore;
    }

    public void setScore(int score) {
        mScore = score;
    }

    public void showScore() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this,"number",0,mScore);
        objectAnimator.setDuration(800);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int score = ((Float) animation.getAnimatedValue()).intValue();
                setText(String.valueOf(score));
            }
        });
        objectAnimator.start();
    }
}
